#include "DAL.h"
#include "DALUtils.h"
#include "DALCache.h"
#include "DALFileAccess.h"

int EstablishDataEnvironment(String DirectoryPath, EntityTable EntityTable)
{

	//Environment already established
	if (EnvironmentDirectoryPath != NULL)
		return -1;

	//DirectoryPath is illegal...
	if (!CheckDirectoryPath(DirectoryPath))
		return -1;

	if (!CheckEntityTable(EntityTable))
		return -1;

	EnvironmentDirectoryPath = malloc(strlen(DirectoryPath) + 1);

	strcpy(EnvironmentDirectoryPath, DirectoryPath);
	EnvironmentEntityTable = EntityTable;

	AllocateMemories();

	CreateEntityCaches();
	EstablishFileSystem();

	return 0;
}

void DestructDataEnvironment()
{
	DestructEntityCaches();
	FreeFileSystem();

	FreeMemories();

	free(EnvironmentDirectoryPath);
	DestructEntityTable(EnvironmentEntityTable);
}